﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// news_index 的摘要说明
    /// </summary>
    public class news_index : IHttpHandler, IRequiresSessionState
    {

        protected int totalCount = 0;
        protected int page = 1;
        protected int pageSize = 0;
        public void ProcessRequest(HttpContext context)
        {
            this.pageSize = 15;
            this.page = DTRequest.GetQueryInt("page", 1);

            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
            //获取供应信息
            BLL.news bll = new BLL.news();
            DataSet ds = bll.GetList(this.pageSize, this.page, "", "id desc", out this.totalCount);
            int total_page = this.totalCount / this.pageSize;
            strTxt.Append("\"total_page\":" + total_page.ToString());

            if (this.page > total_page)
            {
                strTxt.Append(",\"news_list\":\"\"");
            }
            else
            {
                //如果记录存在
                if (ds.Tables[0].Rows.Count > 0)
                {
                    strTxt.Append(",\"news_list\":[");
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr3 = ds.Tables[0].Rows[i];
                        strTxt.Append("{");
                        strTxt.Append("\"id\":" + dr3["id"]);
                        strTxt.Append(",\"title\":\"" + dr3["title"] + "\"");
                        strTxt.Append(",\"description\":\"" + Microsoft.JScript.GlobalObject.escape(dr3["description"].ToString()) + "\"");
                        if (string.IsNullOrEmpty(dr3["img_url"].ToString()))
                        {
                            strTxt.Append(",\"img_url\":\"../image/error-img.png\"");
                        }
                        else
                        {
                            strTxt.Append(",\"img_url\":\"http:\\/\\/www.shicaiquanzi.com" + dr3["img_url"] + "\"");
                        }
                        strTxt.Append("}");
                        //是否加逗号
                        if (i < ds.Tables[0].Rows.Count - 1)
                        {
                            strTxt.Append(",");
                        }
                    }
                    strTxt.Append("]");
                }
            }
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}